﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Objects;

namespace PoserLoot.Web.Models
{
    public class EntityRepository : IRepository
    {
        private PoserLootEntities _entities = new PoserLootEntities();

        #region Category methods

        public Category CreateCategory(Category category)
        {
            _entities.AddToCategory(category);
            _entities.SaveChanges();

            return category;
        }

        public void DeleteCategory(Category category)
        {
            _entities.DeleteObject(category);
            _entities.SaveChanges();
        }

        public Category EditCategory(Category category)
        {
            var originalCategory = this.GetCategory(category.CategoryId);

            // Save changes
            _entities.ApplyPropertyChanges(originalCategory.EntityKey.EntitySetName, category);
            _entities.SaveChanges();
            return category;
        }

        public Category GetCategory(Guid categoryId)
        {
            return (from p in _entities.Category
                    where p.CategoryId == categoryId
                    select p).First();
        }

        public IEnumerable<Category> ListCategories()
        {
            return (from p in _entities.Category
                    orderby p.Appellation
                    select p);
        }

        #endregion

        #region Entry methods

        public Entry CreateEntry(Entry entry)
        {
            _entities.AddToEntry(entry);
            _entities.SaveChanges();

            return entry;
        }

        public void DeleteEntry(Entry entry)
        {
            _entities.DeleteObject(entry);
            _entities.SaveChanges();
        }

        public Entry EditEntry(Entry entry)
        {
            var originalEntry = this.GetEntry(entry.EntryId);

            // Save changes
            _entities.ApplyPropertyChanges(originalEntry.EntityKey.EntitySetName, entry);
            _entities.SaveChanges();
            return entry;
        }

        public Entry GetEntry(Guid entryId)
        {
            var q = from p in _entities.Entry
                    where p.EntryId == entryId
                    select p;

            string log = (q as ObjectQuery<Entry>).ToTraceString();

            return q.First();
        }

        public IEnumerable<Entry> ListEntries(Guid? categoryId)
        {
            IEnumerable<Entry> q;

            if (categoryId.HasValue)
                q = from p in _entities.Entry
                    where p.Category.CategoryId == categoryId.Value
                    orderby p.CreateDate descending
                    select p;
            else
                q = (from p in _entities.Entry
                     orderby p.CreateDate descending
                     select p).Take(20).OrderByDescending(p => p.CreateDate);

            return (q);
        }

        public IEnumerable<Entry> ListEntries(string keyword)
        {
            IEnumerable<Entry> q;

            q = from p in _entities.Entry
                where p.Code == keyword || p.Appellation.Contains(keyword)
                orderby p.CreateDate
                select p;
         
            return (q);
        }

        #endregion

        #region File methods

        public File CreateFile(File file)
        {
            _entities.AddToFile(file);
            _entities.SaveChanges();

            return file;
        }

        public void DeleteFile(File file)
        {
            _entities.DeleteObject(file);
            _entities.SaveChanges();
        }

        public File EditFile(File file)
        {
            var originalFile = this.GetFile(file.FileId);

            // Save changes
            _entities.ApplyPropertyChanges(originalFile.EntityKey.EntitySetName, file);
            _entities.SaveChanges();
            return file;
        }

        public File GetFile(Guid fileId)
        {
            return (from p in _entities.File
                    where p.FileId == fileId
                    select p).First();
        }

        public IEnumerable<File> ListFiles(Guid entryId)
        {
            return (from p in _entities.File
                    where p.Entry.EntryId == entryId
                    orderby p.Path
                    select p);
        }

        #endregion

        #region Gallery methods

        public Gallery CreateGallery(Gallery gallery)
        {
            _entities.AddToGallery(gallery);
            _entities.SaveChanges();

            return gallery;
        }

        public void DeleteGallery(Gallery gallery)
        {
            _entities.DeleteObject(gallery);
            _entities.SaveChanges();
        }

        public Gallery EditGallery(Gallery gallery)
        {
            var originalGallery = this.GetGallery(gallery.GalleryId);

            // Save changes
            _entities.ApplyPropertyChanges(originalGallery.EntityKey.EntitySetName, gallery);
            _entities.SaveChanges();
            return gallery;
        }

        public Gallery GetGallery(Guid galleryId)
        {
            return (from p in _entities.Gallery
                    where p.GalleryId == galleryId
                    select p).First();
        }

        public IEnumerable<Gallery> ListGallerys(Guid entryId)
        {
            return (from p in _entities.Gallery
                    where p.Entry.EntryId == entryId
                    orderby p.OrderIndex
                    select p);
        }

        #endregion

        #region Comment methods

        public Comment CreateComment(Comment comment)
        {
            _entities.AddToComment(comment);
            _entities.SaveChanges();

            return comment;
        }

        public void DeleteComment(Comment comment)
        {
            _entities.DeleteObject(comment);
            _entities.SaveChanges();
        }

        public Comment EditComment(Comment comment)
        {
            var originalComment = this.GetComment(comment.CommentId);

            // Save changes
            _entities.ApplyPropertyChanges(originalComment.EntityKey.EntitySetName, comment);
            _entities.SaveChanges();

            return comment;
        }

        public Comment GetComment(Guid commentId)
        {
            return (from p in _entities.Comment
                    where p.CommentId == commentId
                    select p).First();
        }

        public IEnumerable<Comment> ListComments(Guid entryId)
        {
            return (from p in _entities.Comment
                    where p.Entry.EntryId == entryId
                    orderby p.CreateDate
                    select p);
        }

        #endregion
    }
}
